Bluetoothla string gönderme sorunu
29.12.2015 - 07:07
Selamlar, programlama işinde yeniyim, şöyle bi kod var elimde, bi kaç yerden bulduğum kodları derlerdim. Voice recognition ile aldığım sesi bluetooth üzerinden arduinoya göndermek istiyorum ancak sorun şu ki gönderdiğim veriler iletilmiyor. Bluetooth bağlantı ve ses tanıma kısmı sorunsuz çalışmasına rağmen, ses tanımayla aldığım stringi bluetootha gönderemiyorum. ileri geri sağ sol gibi motor kontrolü yapıcam. Arduino kısmını başka uygulamalarla denedim sorunsuz çalışıyor. Kendi yaptığım uygulamada sıkıntı var dediğim gibi
Yardımcı olur musunuz?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.furkan.myapplication.MainActivity"
tools:showIn="@layout/activity_main">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="horizontal" >
</LinearLayout>
<ListView
android:id="@+id/liste"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="64dp">
</ListView>
<ImageButton
android:id="@+id/btnSpeak"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:layout_weight="1"
android:text="Konuşma Butonu"
android:src="@drawable/mn"
android:layout_alignTop="@+id/linearLayout1"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/ss"
android:id="@+id/txttoSpeechInput"
android:layout_below="@+id/btnSpeak"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/result"
android:layout_below="@+id/txttoSpeechInput"
android:layout_alignRight="@+id/btnSpeak"
android:layout_alignEnd="@+id/btnSpeak" />
</RelativeLayout>
package com.example.furkan.myapplication;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.speech.RecognizerIntent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
private ImageButton imageButton;
private TextView txtSpeechInput;
private TextView text;
OutputStream outstream;
InputStream instream;
ArrayAdapter<String> adaptorlist;
private ListView liste;
BluetoothAdapter blt;
Set<BluetoothDevice> arraydevice;
ArrayList<String> eslesen;
ArrayList<BluetoothDevice> aygitlar;
public static final UUID mmuuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
protected static final int baglanti = 0;
protected static final int mesajoku = 1;
IntentFilter filtre;
BroadcastReceiver receiver;
Boolean foundCommand;
private static final String[] commands = {"ileri", "geri", "sağa dön", "sola dön"};
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch(msg.what){
case baglanti:
ConnectedThread connectedThread = new ConnectedThread((BluetoothSocket)msg.obj);
Toast.makeText(getApplicationContext(), "Baglandı", 0).show();
String s = "successfully connected";
break;
case mesajoku:
byte[] readBuf = (byte[])msg.obj;
String string = new String(readBuf);
Toast.makeText(getApplicationContext(), string, 0).show();
break;
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
liste=(ListView)findViewById(R.id.liste);
liste.setOnItemClickListener(this);
adaptorlist = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
liste.setAdapter(adaptorlist);
blt = BluetoothAdapter.getDefaultAdapter();
eslesen = new ArrayList<String>();
filtre = new IntentFilter(BluetoothDevice.ACTION_FOUND);
aygitlar = new ArrayList<BluetoothDevice>();
txtSpeechInput = (TextView)findViewById(R.id.txttoSpeechInput);
receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
aygitlar.add(device);
String s = "";
for(int a = 0; a < eslesen.size(); a++){
if(device.getName().equals(eslesen.get(a))){
s = "(Eslesti)";
break;
}
}
adaptorlist.add(device.getName()+""+s+""+"\n"+device.getAddress());
}
else if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)){
if(blt.getState() == blt.STATE_OFF){
bltac();
}
}
}
};
registerReceiver(receiver, filtre);
filtre = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(receiver, filtre);
if(blt==null){
Toast.makeText(getApplicationContext(), "Bluetooth aygıtları bulunamadı", 0).show();
text.setText("Bluetooth aygıtı yok");
finish();
}
else{
if(!blt.isEnabled()){
bltac();
}
getPairedDevices();
startDiscovery();
imageButton= (ImageButton) findViewById(R.id.btnSpeak);
imageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
promptSpeechInput();
}
});
}
}
private void promptSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, getString(R.string.speech_prompt));
startActivityForResult(intent, 100);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 100: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
foundCommand = false;
for(String command : commands) {
if(result.contains(command)) {
foundCommand = true;
if(command == "ileri") {
txtSpeechInput.setText("geri");
try {
outstream.write("ileri".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
else if(command == "geri") {
txtSpeechInput.setText("geri");
try {
outstream.write("geri".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
else if(command == "sola dön") {
txtSpeechInput.setText("sola dön");
try {
outstream.write("sola dön".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
else if(command == "sağa dön") {
txtSpeechInput.setText("sağa dön");
try {
outstream.write("sağa dön".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
if (!foundCommand) {
txtSpeechInput.setText("Dediğiniz anlaşılamadı!");
}
}
break;
}
}
}
private void startDiscovery() {
// TODO Auto-generated method stub
blt.cancelDiscovery();
blt.startDiscovery();
}
private void bltac() {
// TODO Auto-generated method stub
Intent intent =new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, 1);
}
private void getPairedDevices() {
// TODO Auto-generated method stub
arraydevice = blt.getBondedDevices();
if(arraydevice.size()>0){
for(BluetoothDevice device:arraydevice){
eslesen.add(device.getName());
}
}
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
if(blt.isDiscovering()){
blt.cancelDiscovery();
}
if(adaptorlist.getItem(arg2).contains("Eslesti")){
BluetoothDevice selectedDevice = aygitlar.get(arg2);
ConnectThread connect = new ConnectThread(selectedDevice);
connect.start();
}
else{
Toast.makeText(getApplicationContext(), "Cihazlar eşleşmedi", 0).show();
text.setText("Cihazlar eşleşmedi");
}
}
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
public ConnectThread(BluetoothDevice device) {
BluetoothSocket tmp = null;
mmDevice = device;
try {
tmp = device.createRfcommSocketToServiceRecord(mmuuid);
} catch (IOException e) {
}
mmSocket = tmp;
}
public void run() {
blt.cancelDiscovery();
try {
mmSocket.connect();
} catch (IOException connectException) {
try {
mmSocket.close();
} catch (IOException closeException) { }
return;
}
mHandler.obtainMessage(baglanti, mmSocket).sendToTarget();
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
public ConnectedThread(BluetoothSocket socket) {
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) { }
mmInStream = tmpIn;
outstream = tmpOut;
}
public void run() {
byte[] buffer;
int bytes;
while (true) {
try {
buffer = new byte[1024];
bytes = mmInStream.read(buffer);
mHandler.obtainMessage(mesajoku, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
break;
}
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) { }
}
}
}
14
Görüntülenme
0 Beğeni